from django import forms
from web.forms.bootstrap import BootStrapForm
from web import models


class IssuesModel(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'creator_datetime', 'latest_update_datetime']

        widgets = {
            'assign': forms.Select(attrs={'class': 'selectpicker', 'data-live-search': 'true'}),
            'attention': forms.SelectMultiple(attrs={'class': 'selectpicker', 'data-live-search': 'true', 'data-actions-box': 'true'}),
            'parent': forms.Select(attrs={'class': 'selectpicker', 'data-live-search': 'true'}),
        }

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 处理初始化问题

        # 1 获取当前项目的项目问题
        # values_list [(id, 'title),()]
        self.fields['issues_type'].choices = models.IssuesType.objects.filter(project=request.bug_platform.project).values_list('id', 'title')

        # 2 获取当前项目的模块
        module_list = [('', '没有选择任何项'), ]
        module_object_list = self.fields['module'].choices = models.Module.objects.filter(project=request.bug_platform.project).values_list('id', 'title')
        module_list.extend(module_object_list)  # 添加到后面
        self.fields['module'].choices = module_list

        # 3 指派和关注着
        # 数据库中找到项目参与者和创建者
        total_user_list = [(request.bug_platform.project.creator_id, request.bug_platform.project.creator.username), ]  # 创建者先手动加入
        project_user_list = models.ProjectUser.objects.filter(Project=request.bug_platform.project).values_list('user_id', 'user__username')  # 项目参与者

        total_user_list.extend(project_user_list)

        self.fields['assign'].choices = [('', '没有选择任何项目')] + total_user_list
        self.fields['attention'].choices = total_user_list

        # 当前项目自己创建的问题
        parent_list = [('', '没有选择任何项目')]
        parent_object_list = models.Issues.objects.filter(project=request.bug_platform.project).values_list('id', 'subject')
        parent_list.extend(parent_object_list)
        self.fields['parent'].choices = parent_list


class IssuesReplyModelForm(forms.ModelForm):
    class Meta:
        model = models.IssuesReply
        fields = ['content', 'reply']


class InviteModelForm(BootStrapForm, forms.ModelForm):
    """ 邀请成员 """

    class Meta:
        model = models.ProjectInvite
        fields = ['period', 'count']
